package com.qc.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Random;

public class EncryptionService {

	private ArrayList<MessageDigest> _pool; 
	private static EncryptionService _self;
	private final static int Threads = 8;
	public static EncryptionService getInstance(){
		if(_self == null){
			_self = new EncryptionService();
			_self._pool = new ArrayList<MessageDigest>();
			
			try {
				for(int i=0; i < Threads; i++){
					_self._pool.add(MessageDigest.getInstance("MD5"));
				}
			} catch (NoSuchAlgorithmException e) {
//				Util.logFatal(e);
				throw new Error(e);
			}

		}
		return _self;
	}
	private Random r = new Random();
	public MessageDigest getDigest(){
		int i = r.nextInt();
		if(i < 0) i *= -1;
		return _pool.get(i%Threads);
	}
}
